<?php

  abstract class StringUtils {

    /**
     * Determine whether a variable is empty.
     *
     * @param String $string String to be checked.
     * @return Boolean Returns FALSE if $string is not empty.
     * @link http://be2.php.net/manual/en/function.empty.php
     * @link http://be2.php.net/manual/en/function.is-string.php
     */
    public static function isEmpty(IString $string) {
      return StringUtils::Trim($string)->toString() == String::EMPTY_STRING ? new Boolean(true) : new Boolean(false);
    }

    /**
     * Strip whitespace (or other characters) from the beginning and end of a string.
     *
     * @param String $string The string that will be trimmed.
     * @param String $charlist Optionally, the stripped characters can also be specified using the charlist parameter.
     * Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
     * @return String The trimmed string.
     * @link http://be2.php.net/manual/en/function.trim.php
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-null.php
     */
    public static function trim(IString $string, String $charlist = null) {
      return $charlist == null ? new String(trim($string->toString())) : new String(trim($string->toString(), $charlist->toString()));
    }

    /**
     * Binary safe string comparison.
     *
     * @param String $string1 The first string.
     * @param String $string2 The second string.
     * @return Integer Returns < 0 if str1 is less than str2 ; > 0 if str1 is greater than str2 , and 0 if they are equal.<br />
     * @link http://be2.php.net/manual/en/function.strcmp.php
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.strcmp.php
     */
    public static function equals(IString $string1, IString $string2) {
      return new Integer(strcmp($string1->toString(), $string2->toString()));
    }

    /**
     * Check if a String starts with a specified prefix.
     *
     * @param String $string The string to be analized.
     * @param String $prefix The prefix to found at the beginning of $string.
     * @return Boolean Returns TRUE if $prefix match the start of the string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.substr.php
     * @link http://be2.php.net/manual/en/function.strlen.php
     */
    public static function startsWith($string, $prefix) {
      return substr($string->toString(), 0, strlen($prefix->toString())) == $prefix->toString() ? new Boolean(true) : new Boolean(false);
    }

    /**
     * Check if a String ends with a specified suffix.
     *
     * @param String $string The string to be analized.
     * @param String $suffix The suffix to found at the end of $string.
     * @return Boolean Returns TRUE if $suffix match the end of the string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.substr.php
     * @link http://be2.php.net/manual/en/function.strlen.php
     */
    public static function endsWith(IString $string, IString $suffix) {
      return substr($string->toString(), strlen($string->toString()) - strlen($suffix->toString())) == $suffix->toString() ? new Boolean(true) : new Boolean(false);
    }

    /**
     * Find position of first occurrence of a string.
     *
     * @param String $string The string to search in.
     * @param String $needle If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
     * @param Integer $start The optional $start parameter allows you to specify which character in $string to start searching.
     * The position returned is still relative to the beginning of $string.
     * @return IInteger Returns the position. If $needle is not found, IndexOf() will return -1.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.strpos.php
     */
    public static function indexOf(IString $string, IString $needle, IInteger $start = null) {
      $index = $start == null ? strpos($string->toString(), $needle->toString(), 0) : strpos($string->toString(), $needle->toString(), $start->integerValue());
      return $index == false ? new Integer(-1) : new Integer($index);
    }

    /**
     * Find position of last occurrence of a char in a string.
     *
     * @param String $string The string to search in.
     * @param String $needle If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
     * @param Integer $end May be specified to begin searching an arbitrary number of characters into the string from the end.
     * This parameter must be negative to take effect.
     * @return Integer Returns the position where the needle exists. Returns -1 if the $needle was not found.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-int.php
     * @link http://be2.php.net/manual/en/function.strrpos.php
     */
    public static function lastIndexOf(IString $string, IString $needle, IInteger $end = null) {
      $index = $end == null ? strrpos($string->toString(), $needle->toString()) : strrpos($string->toString(), $needle->toString(), $end->integerValue());
      return $index == false ? new Integer(-1) : new Integer($index);
    }

    /**
     * Checks if String contains a search character or string.
     *
     * @param String $string The string to search in.
     * @param String $search The character or string to search for.
     * @return Boolean TRUE is $search is found is $string, FALSE otherwise.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-int.php
     * @link http://be2.php.net/manual/en/function.strrpos.php
     */
    public static function contains(IString $string, IString $search) {
      return strpos($string->toString(), $search->toString()) === FALSE ? new Boolean(false) : new Boolean(true);
    }

    /**
     * Returns the portion of string specified by the start and length parameters.
     *
     * @param String $string The input string.
     * @param Integer $start The start position to cut.
     * @param Integer $length The length of the string top extract.
     * @return String Returns the extracted part of string or FALSE on failure.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-int.php
     * @link http://be2.php.net/manual/en/function.substr.php
     */
    public static function substring(IString $string, IInteger $start, IInteger $length = null) {
      return $length == null ? new String(substr($string->toString(), $start->integerValue())) : new String(substr($string->toString(), $start->integerValue(), $length->integerValue()));
    }

    /**
     * Gets the rightmost length characters of a string.
     *
     * @param String $string The input string.
     * @param Integer $length The length of the string to extract.
     * @return String Returns the extracted part of string or FALSE on failure.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-int.php
     * @link http://be2.php.net/manual/en/function.substr.php
     */
    public static function right(IString $string, IInteger $length) {
      return new String(strrev(substr(strrev($string->toString()), $length->integerValue())));
    }

    /**
     * Splits the provided text into an array, using whitespace as the separator.
     *
     * @param String $string The input string.
     * @param String $pattern The pattern used for the split.
     * @param Integer $limit If limit is set, the returned array will contain a maximum of limit elements with the last element containing the whole rest of string .
     * @return array Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the case-sensitive regular expression pattern.<br />
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-int.php
     * @link http://be2.php.net/manual/en/function.explode.php
     */
    public static function split(IString $string, IString $pattern = null, IInteger $limit = null) {
      return explode($pattern == null ? " " : $pattern->toString(), $string->toString(), $limit == null ? 999 : $limit->integerValue());
    }

    /**
     * Join array elements with a string.
     *
     * @param array $array The array of strings to implode.
     * @param String $separator Defaults to a space char.
     * @return String Returns a string containing a string representation of all the array elements in the same order, with the $separator string between each element.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-array.php
     * @link http://be2.php.net/manual/en/function.implode.php
     */
    public static function join(array $array, IString $separator = null) {
      return new String(implode($separator == null ? " " : $separator->toString(), $array));
    }

    /**
     * Remove the last character from a String.
     *
     * @param String $string The input string.
     * @return String The same string without the last character.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.strlen.php
     * @link http://be2.php.net/manual/en/function.substr.php
     */
    public static function chop(IString $string) {
      return substr($string->toString(), 0, strlen($string->toString()) - 1);
    }

    /**
     * Pad a string to a certain length with another string.
     * This functions returns the input string padded on the left and the right with characters up to the limit.
     *
     * @param String $string The input string.
     * @param String $pattern The $pattern may be truncated if the required number of padding characters can't be evenly divided by the pattern's length.
     * @param Integer $length If the value of $length is negative, less than, or equal to the length of the input string, no padding takes place.
     * @return String Returns the padded string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-int.php
     * @link http://be2.php.net/manual/en/function.str-pad.php
     */
    public static function pad(IString $string, IString $pattern, IInteger $length) {
      return new String(str_pad($string->toString(), $length->integerValue(), $pattern->toString(), STR_PAD_BOTH));
    }

    /**
     * Pad a string to a certain length with another string.
     * This functions returns the input string padded on the left with characters up to the limit.
     *
     * @param string $string The input string.
     * @param string $pattern The $pattern may be truncated if the required number of padding characters can't be evenly divided by the pattern's length.
     * @param integer $length If the value of $length is negative, less than, or equal to the length of the input string, no padding takes place.
     * @return string Returns the padded string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-int.php
     * @link http://be2.php.net/manual/en/function.str-pad.php
     */
    public static function padLeft(IString $string, IString $pattern, IInteger $length) {
      return new String(str_pad($string->toString(), $length->integerValue(), $pattern->toString(), STR_PAD_LEFT));
    }

    /**
     * Pad a string to a certain length with another string.
     * This functions returns the input string padded on the right with characters up to the limit.
     *
     * @param String $string The input string.
     * @param String $pattern The $pattern may be truncated if the required number of padding characters can't be evenly divided by the pattern's length.
     * @param Integer $length If the value of $length is negative, less than, or equal to the length of the input string, no padding takes place.
     * @return String Returns the padded string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-int.php
     * @link http://be2.php.net/manual/en/function.str-pad.php
     */
    public static function padRight(IString $string, IString $pattern, IInteger $length) {
      return new String(str_pad($string->toString(), $length->integerValue(), $pattern->toString(), STR_PAD_RIGHT));
    }

    /**
     * Make a string uppercase.
     *
     * @param String $string The input string.
     * @return String Returns the uppercased string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.strtoupper.php
     */
    public static function toUpper(IString $string) {
      return new String(strtoupper($string->toString()));
    }

    /**
     * Make a string lowercase.
     *
     * @param String $string The input string.
     * @return String Returns the lowercased string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.strtolower.php
     */
    public static function toLower(IString $string) {
      return new String(strtolower($string->toString()));
    }

    /**
     * Reverse a string.
     *
     * @param String $string The string to be reversed.
     * @return String Returns the reversed string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.strrev.php
     */
    public static function reverse(IString $string) {
      return new String(strrev($string));
    }

    /**
     * Strip whitespace (or other characters) from the beginning of a string.<br />
     *
     * @param String $string The input string.
     * @param String $charlist You can also specify the characters you want to strip, by means of the charlist parameter.
     * Simply list all characters that you want to be stripped.
     * With .. you can specify a range of characters.
     * @return String Returns the modified string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-null.php
     * @link http://be2.php.net/manual/en/function.ltrim.php
     */
    public static function trimStart(IString $string, IString $charlist = null) {
      return $charlist == null ? new String(ltrim($string->toString())) : new String(ltrim($string->toString(), $charlist->toString()));
    }

    /**
     * Strip whitespace (or other characters) from the end of a string.
     *
     * @param String $string The input string.
     * @param String $charlist You can also specify the characters you want to strip, by means of the charlist parameter.
     * Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
     * @return String Returns the modified string.
     * @link http://be2.php.net/manual/en/function.is-string.php
     * @link http://be2.php.net/manual/en/function.is-null.php
     * @link http://be2.php.net/manual/en/function.rtrim.php
     */
    public static function trimEnd(IString $string, IString $charlist = null) {
      return $charlist == null ? new String(rtrim($string->toString())) : new String(rtrim($string->toString(), $charlist->toString()));
    }

  }

?>
